package com.example.april.controller;

import com.example.april.service.OAuth2Service;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;
@Controller
@RequestMapping("/OAuth2")
public class OAuth2Controller {

    @Autowired
    OAuth2Service oAuth2Service;

    @GetMapping("/github/callback")
    public String githubCallback(@RequestParam("code") String code, HttpServletRequest request) throws Exception {
        Map<String, String> authorInfo = oAuth2Service.githubCallback(code);
        request.getSession().setAttribute("id",authorInfo.get("id"));
        request.getSession().setAttribute("name",authorInfo.get("name"));
        return "redirect:/index";
    }


    @GetMapping("/gitee/callback")
    public String giteeCallback(@RequestParam("code") String code, HttpServletRequest request) throws Exception {
        Map<String, String> authorInfo = oAuth2Service.giteeCallback(code);
        request.getSession().setAttribute("id",authorInfo.get("id"));
        request.getSession().setAttribute("name",authorInfo.get("name"));
        return "redirect:/index";
    }

}